﻿using System;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace AsyncControllerSession.Controllers
{
    // Author: Otto Neff | webapps-in-action.com
    public class GetInfoController : Controller
    {
        public async Task<ActionResult> Async(string id)
        {
            if (Session["login"] != null)
            {
                Random r = new Random();
                int delayed = r.Next(500, 1000);
                System.Threading.Thread.Sleep(delayed);
                dynamic result = new
                {
                    callId = id,
                    dateTimeUtc = DateTime.UtcNow.ToString("r"),
                    text = "SomeString",
                    money = (decimal)40.2,
                    guid = Guid.NewGuid(),
                    delayed = delayed
                };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("Puuuhh login first", JsonRequestBehavior.AllowGet);
            }
        }
    }
}
